package com.nanfang51g3.ckz.ui.welcome;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.service.ImmortalService;
import com.nanfang51g3.ckz.ui.H5WebView;
import com.nanfang51g3.ckz.ui.home.HomeMainActivity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SystemBarTintManager;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/4/19.
 */
public class WelcomeActivity extends AppCompatActivity implements WelcomeContract.View {
    WelcomeContract.WelcomeActionsListener welcomeActionsListener;

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 全屏幕，状态栏透明
     */
    public void initThemeColor() {
        //判断SDK版本是否大于或等于19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.theme_transparent);//通知栏所需颜色
    }
    void startService() {
        KLog.i("Evd", "---startService");
        Intent intent = new Intent(this, ImmortalService.class);
        startService(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initThemeColor();
        AppManager.getAppManager().addActivity(this);
        welcomeActionsListener = DaggerWelcomeContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .welcomePresenterModule(new WelcomePresenterModule(this))
                .build().getWelcomePresenter();
        KLog.i("TAG", welcomeActionsListener == null ? "welcomeActionsListener==null" : "welcomeActionsListener!=null");
        ButterKnife.bind(this);



        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this, HomeMainActivity.class));
                AppManager.getAppManager().finishActivity(WelcomeActivity.this);
//                startActivity(new Intent(WelcomeActivity.this, H5WebView.class).putExtra("id_title",R.string.app_name).putExtra("id_url","file:///android_asset/input.html"));
//                AppManager.getAppManager().finishActivity(WelcomeActivity.this);
            }
        }, 3000);
//        welcomeActionsListener.writeStreamLog();
//        写入服务器返回的app错误日志。
//        welcomeActionsListener.writeStreamLog();
        /**
         * @see  WelcomePresenter#isDownloadAPatch()
         */
//        welcomeActionsListener.isDownloadAPatch();


    }

    @Override
    public void download(String fileName, String path_v) {
        /**
         * @see  WelcomePresenter#downloadAppAPatch(String, String)
         */
        welcomeActionsListener.downloadAppAPatch(fileName, path_v);

    }

    @Override
    public void startView() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this, HomeMainActivity.class));
                AppManager.getAppManager().finishActivity(WelcomeActivity.this);
            }
        }, 1000);
    }

    @Override
    public Context getContext() {
        return this;
    }

    Handler handler = new Handler();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
